<template>
	<!-- 我的消息 -->
	<view class="my-information">
		<!-- tab切换 -->
		<uni-segmented-control :current="current" :values="items" :style-type="styleType" :active-color="activeColor"
			@clickItem="onClickItem" />
		<view class="content">
			<!-- 加载中效果 -->
			<uni-load-more v-if="loading" status="loading" />

			<view v-if="!loading && itemData.length > 0">
				<view class="row" v-for="(item,index) in itemData" :key="index">
					<view class="left">
						<view class="head" @click="toPage(item.createUserMember)">
							<image class="profilephoto" :class="item.isVip?'profilephoto-vip':''"
								:src="item.profilephoto" mode="widthFix"></image>
							<!-- VIP -->
							<image class="vip" v-if="item.isVip" src="/static/images/vip.png" mode="widthFix"></image>
						</view>
						<view class="text-w">
							<view class="name">{{ item.name }}</view>
							<view class="text">{{ item.msgType === '0'?'开始关注了你':'评论了你的文字' }}</view>
						</view>
					</view>
					<view class="right">{{ item.date }}</view>
					<!-- 查看原文 -->
					<view v-if="item.msgType !== '0'" class="ckyw" @click="ckywCliack(item.msgJumpId)"> 查看原文</view>
				</view>
			</view>

			<!-- 暂无数据显示 -->
			<view class="zwxx-img" v-if="!loading && itemData.length === 0">
				<image src="https://luntan.xiaouyf.cn/uecs-wechat/static/img/qianduan/zwxx.png" mode="widthFix"></image>
				<view>暂无数据</view>
			</view>
		</view>
		<!-- 底部菜单 -->
		<tab-bar :selected='3'></tab-bar>
	</view>
</template>

<script>
	import TabBar from '../../components/TabBar.vue';
	import uniSegmentedControl from '@/uni_modules/uni-segmented-control/components/uni-segmented-control/uni-segmented-control.vue'

	export default {
		components: {
			uniSegmentedControl,
			TabBar
		},
		data() {
			return {
				items: ['全部', '关注', '评论'],
				current: 0,
				styleType: 'text',
				activeColor: '#AC6C17',
				itemData: [],
				loading: false // 添加loading状态
			}
		},
		onLoad() {
			this.init('2') // 查看全部
		},
		methods: {
			toPage(item) {
				console.log(item)
				uni.navigateTo({
					url: `/pages/my-card/my-card?id=${item.guid}`
				})
			},
			// 查看原文-跳转论坛列表页
			ckywCliack(msgJumpId) {
				uni.navigateTo({
					url: `/pages/luntan/luntan?guid=${msgJumpId}`
				})
			},
			// 获取消息列表-全部
			async init(id) {
				this.loading = true // 开始加载
				try {
					const data = {
						userId: this.$store.state.user.userInfo.guid,
						type: id
					}
					const res = await this.$api.getMsgGetList(data);
					if (res.succ && res.data) {
						this.itemData = res.data.map(item => {
							return {
								profilephoto: item.createUserMember.avatar,
								name: item.createUserMember.realName,
								authenticated: item.isVerified,
								isVip: item.createUserMember.authStatus == '1' || item.createUserMember.authStatus == '2',
								msgType: item.msgType,
								date: item.createTimeCn,
								msgJumpId: item.msgJumpId,
								createUserMember: item.createUserMember
							}
						})
						console.log(this.itemData)
					} else {
						this.itemData = []
					}
				} catch (error) {
					console.error('获取消息列表失败:', error)
					this.itemData = []
				} finally {
					this.loading = false // 结束加载
				}
			},
			// tab切换
			onClickItem(index) {
				const currentIndex = Number(index.currentIndex);
				if (this.current !== currentIndex) {
					this.current = currentIndex;
					console.log(this.current)
					if (this.current === 0) {
						this.init('2')
					} else if (this.current === 1) {
						this.init('0')
					} else if (this.current === 2) {
						this.init('1')
					}
				}
			}
		}
	}
</script>

<style lang="less">
	.my-information {
		height: 100%;
		display: flex;
		flex-direction: column;
		background: linear-gradient(180deg, #FAEBDB 0%, rgba(255, 247, 238, 0) 500rpx);

		.segmented-control {
			position: relative;
			height: 100rpx !important;

			&::after {
				z-index: -1;
				content: '';
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 1rpx;
			}

			.segmented-control__text {
				padding: 15px 10rpx !important;
				font-size: 32rpx !important;
			}

			.segmented-control__item--text {
				position: relative;

				&::after {
					z-index: 2;
					content: '';
					position: absolute;
					left: 10rpx;
					right: 10rpx;
					bottom: -1px;
					height: 4rpx;
					background: #AC6C17;
				}
			}
		}
	}

	.content {
		margin: 10rpx 30rpx;
		flex: 1;
		overflow: auto;

		.row {
			position: relative;
			display: flex;
			padding: 45rpx 0;

			&::after {
				content: '';
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 1rpx;
				background: rgba(2, 2, 2, .1);
				transform: translateY(.5);
			}

			&:last-child {
				&::after {
					display: none;
				}
			}

			.left {
				display: flex;
				align-items: center;
				flex: 1;

				.head {
					position: relative;

					.profilephoto {
						width: 100rpx;
						height: 100rpx;
						border: 5rpx solid #fff;
						border-radius: 100%;

						&.profilephoto-vip {
							border: 5rpx solid #E09E40;
							border-radius: 100%;
						}
					}

					.vip {
						width: 84rpx;
						position: absolute;
						left: 50%;
						bottom: 4rpx;
						margin-left: -42rpx;
					}
				}

				.text-w {
					display: flex;
					flex-direction: column;
					margin-left: 28rpx;

					.name {
						font-size: 32rpx;
						color: #020C07;
					}

					.text {
						font-size: 26rpx;
						margin-top: 30rpx;
						color: #444;
					}
				}
			}

			.right {
				font-size: 24rpx;
				color: #666;
				margin-top: 78rpx;
			}

			.ckyw {
				position: absolute;
				right: 0;
				top: 50rpx;
				width: 112rpx;
				height: 38rpx;
				line-height: 38rpx;
				text-align: center;
				font-weight: 300;
				font-size: 20rpx;
				color: #AC6C17;
				border-radius: 16rpx;
				border: 1px solid #AC6C17;
			}
		}
	}

	// 添加无数据样式
	.zwxx-img {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;

		image {
			width: 300rpx;
		}

		view {
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #999;
		}
	}
</style>